package com.yinhai.message.controller;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yinhai.core.app.ta3.web.controller.BaseController;
import com.yinhai.message.api.web.vo.TMResultVo;
import com.yinhai.message.core.bpo.IMessageSendBpo;

/**
 * @author yanglq 2017.3.28
 */
@RequestMapping("messageHttp")
@Controller
public class MessageHttpController extends BaseController {

    @Resource(name = "messageSendBpo")
    private IMessageSendBpo messageSendBpo;

    private static final String SAVE_ROOT_FOLDER = "/ueditor/upload/file";

    @RequestMapping(value = "messageHttpController!sendMessage.do", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String sendMessgae(@RequestParam(name = "msgjsonstr", required = false) String jsonString,
                              @RequestParam(name = "ticket", required = false) String ticket,
                              @RequestParam(name = "file", required = false) MultipartFile[] files) {

        TMResultVo result = new TMResultVo();
        try {
            messageSendBpo.addMessageWidthFiles(jsonString, files, getRequest().getSession().getServletContext().getRealPath("/"), ticket);
            result.setState("1").setSuccess(true).setMessage("调用成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setState("-1").setSuccess(false).setMessage(e.getMessage());
        }
        return com.alibaba.fastjson.JSON.toJSONString(result);
    }

    @RequestMapping(value = "messageHttpController!sendMessageWithMap.do", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String sendMessgaeWithMap(@RequestParam(name = "msgjsonstr", required = false) String jsonStr,
                                     @RequestParam(name = "file", required = false) String files) {
        TMResultVo result = new TMResultVo();
        try {
            String fpath = null;
            List<UeditorFileVo> ufiles = null;
            if (files != null && files.length() > 0) {
                ufiles = com.alibaba.fastjson.JSON.parseArray(files, UeditorFileVo.class);
                for (UeditorFileVo u : ufiles) {
                    String path = u.getPath();
                    fpath = getRequest().getSession().getServletContext().getRealPath(SAVE_ROOT_FOLDER);
                    u.setPath(fpath + File.separator + path.substring(path.lastIndexOf("/") + 1));
                }
            }
            messageSendBpo.addMessageWidthServerFiles(jsonStr, ufiles, getRequest().getSession().getServletContext().getRealPath("/"));
            result.setState("1").setSuccess(true).setMessage("调用成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setState("-1").setSuccess(false).setMessage(e.getMessage());
        }
        return com.alibaba.fastjson.JSON.toJSONString(result);
    }

    @RequestMapping(value = "messageHttpController!sendAppMessage.do", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String sendAppMessgae(@RequestParam(name = "msgjsonstr", required = false) String jsonString,
                                 @RequestParam(name = "aac001", required = true) String aac001) {

        TMResultVo result = new TMResultVo();
        try {
            messageSendBpo.addAppMesssage(jsonString, aac001);
            result.setState("1").setSuccess(true).setMessage("调用成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setState("-1").setSuccess(false).setMessage(e.getMessage());
        }
        return com.alibaba.fastjson.JSON.toJSONString(result);
    }

	/*@RequestMapping(value = "messageHttpController!webSocketLogin.do")
	@ResponseBody
	public String webSocketLogin(HttpSession session,
								 @RequestParam(name = "userid", required = true) String userid) {
		System.out.println("登录接口,userId="+userid);
		session.setAttribute(Constants.WEBSOCKET_USERID, userid);
		System.out.println(session.getAttribute(Constants.WEBSOCKET_USERID));
    	return "success";
	}*/
}
